package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.Post;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.requestJson.FuzzyQueryPostReq;
import com.example.demo.entity.responseJson.ListResult;
import com.example.demo.entity.responseJson.QueryPostRes;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.PostMapper;
import com.example.demo.service.PostService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 帖子基本信息表(Post)Service层
 *
 * @author 杨丽辉
 * @since 2021-03-12 17:26:58
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class PostServiceImpl implements PostService {
        @Autowired
        private PostMapper postMapper;


 /**
     * 通过ID和其他条件查询单条数据
     *
     * @param post 实例对象
     * @return 实例对象
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
     public Post queryPostLimit1(Post post){
     return  postMapper.queryPostLimit1( post);
     }
     
 /**
     * 查询指定行数据
     * @param post 实例对象
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public   List<Post> queryAllPostByLimit( Post post, Integer offset,  Integer limit){
   return postMapper.queryAllPostByLimit(post, offset,limit);
  }

  /**
     * 查询数据条数
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public int countAllPost(Post post){
   return postMapper.countAllPost( post);
   }
   /**
     * 通过实体作为筛选条件查询
     *
     * @param post 实例对象
     * @return 对象列表
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public List<Post> queryAllPost(Post post){
   return postMapper.queryAllPost(post);
   }
  /**
     * 新增数据
     *
     * @param post 实例对象
     * @return 影响行数
     */
     @Override
   @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public int insertPost(Post post){
    return  postMapper.insertPost( post);
    }
  /**
     * 修改数据
     *
     * @param post 实例对象
     * @return 影响行数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
     public  int updatePost(Post post){
         return postMapper.updatePost( post);
     }

    /**
     * 通过主键删除数据
     * @param postId 主键
     * @return 影响行数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  int deletePostById(String postId){
   return  postMapper.deletePostById( postId);
   }
     /**
     * 通过主键列表删除，列表长度不能为0
     * @param list 集合
     * @return 条数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public  int deletePostByIds( List<String> list){
    return  postMapper.deletePostByIds(  list);
  }
    
       /**
     * 通过主键修改实体列表，列表长度不能为0，注意：当实体属性为null时，对应的列也会别更新为null
     * @param list 集合
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public int updatePostBatch( List<Post> list){
   return  postMapper.updatePostBatch(list);
   }
    /**
     * 批量其他列,除主键以外的列，列表长度不能为0,字段是空的就置为null
     * @param list 集合
     * @return 条数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
 public   int insertPostBatch( List<Post> list){
 return postMapper.insertPostBatch( list);
 }
      /**
     * 通过实体非空属性删除
     * @param post  实体
     * @return 条数
     */
        @Override
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public   int deletePostByEntity( Post post){
  return  postMapper.deletePostByEntity(  post);
  }
    /**
     * 通过实体不为空的属性作为筛选条件查询列表
     * @param post  实体
     * @return list
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  List<Post> listPostByEntity(Post post){
   return postMapper.listPostByEntity( post);
   }

 /**
     * 新增实体属性不为null的列
     * @param post  实体
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  int insertPostSelect( Post post){
      return postMapper.insertPostSelect( post);

   }
   
      /**
     * 批量新增新增实体属性不为null的列
     * @param list  实体
     * @return 条数
     */
        @Override
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public  int insertPostListSelect( List<Post> list){

     return postMapper.insertPostListSelect( list);
      }
     /**
     * 通过实体不为空的属性作为筛选条件查询单个
     * @param post  实体
     * @return 单个
     */
     @Override
   @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public  Post getPostByEntity(Post post){
     return  postMapper.getPostByEntity(post);
    }

    /**
    * 查询单独帖子详情
    * */
    @Override
    public BackResponseEntity<List<QueryPostRes>> queryPost(DeleteReq deleteReq){

        String id = deleteReq.getId();

        List<QueryPostRes> list = postMapper.queryPost(Wrappers.<Post>query().eq("post_id",id)
                                                                             .orderByDesc("creat_time"));

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, list, "");
    }

    /**
     * 查看作者的帖子
     */

    @Override
    public BackResponseEntity<ListResult<List<QueryPostRes>>> queryPostByAuthor(DeleteReq deleteReq){
        String id = deleteReq.getId();

        long count = PageHelper.count(() -> postMapper.selectList(Wrappers.<Post>query().eq("author_id",id)
                .orderByDesc("creat_time")));
        List<QueryPostRes> list = postMapper.queryPost(Wrappers.<Post>query().eq("author_id",id)
                .orderByDesc("creat_time"));
        ListResult<List<QueryPostRes>> listResult = new ListResult((int)count, list);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, listResult, "");
    }

    /**
     * 模糊查询
     * */
    @Override
    public BackResponseEntity<List<QueryPostRes>> fuzzyqueryPost(FuzzyQueryPostReq fuzzyQueryPostReq){
        List<QueryPostRes> list = postMapper.queryPost(
                Wrappers.<Post>query().like(!ObjectUtils.isEmpty(fuzzyQueryPostReq.getNickName()), "login_nickname",fuzzyQueryPostReq.getNickName())
                        .like(!ObjectUtils.isEmpty(fuzzyQueryPostReq.getPostName()) ,"post_name",fuzzyQueryPostReq.getPostName())
                        .like(!ObjectUtils.isEmpty(fuzzyQueryPostReq.getPostOnelabel()), "post_onelabel",fuzzyQueryPostReq.getPostOnelabel())
                        .like(!ObjectUtils.isEmpty(fuzzyQueryPostReq.getPostTwolabel()) ,"post_twolabel",fuzzyQueryPostReq.getPostTwolabel())
                .orderByDesc("creat_time"));

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, list, "");
    }
}

